package com.carkeeper.subject.service.impl;

import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.subject.pojo.Merchant;
import com.carkeeper.subject.service.AuditService;
import com.carkeeper.subject.service.StoreService;
import com.github.pagehelper.Page;
import com.carkeeper.subject.dao.StoreMapper;
import com.carkeeper.subject.pojo.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.PageHelper;

import java.util.Map;


/**
 * @author chengxihua
 * @version 1.0
 * @date 2020/11/23 18:01
 */
@Service
public class AuditServiceImpl implements AuditService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreService storeService;

    /**
     * 分页
     * @param searMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Store> findPage(Map<String, Object> searMap, int page, int size) {
        page = judgePageParameter(page);
        size = judgeSizeParameter(size);
        PageHelper.startPage(page,size);

        Store store = new Store();
        store.setAuditStatus(StatusCode.STORE_status_NO+"");

        Example example = exampleBySelect(searMap);
        return (Page<Store>) storeMapper.selectByExample(example);
    }

    /**
     * 查询审核拒绝字段
     * @param
     * @return
     */
    @Override
    public Page<Store> findByAuditStatusInReject(Map<String, Object> searMap,int page, int size) {
        page = judgePageParameter(page);
        size = judgeSizeParameter(size);
        PageHelper.startPage(page,size);

        Example example=new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        searMap.put("auditStatus",StatusCode.STORE_status_ERRO);
        criteria.andEqualTo("auditStatus",searMap.get("auditStatus"));


        return (Page<Store>) storeMapper.selectByExample(example);
    }

    /**
     * 查询提交审核数据
     * @param
     * @return
     */
    @Override
    public Page<Store> findByCommit(Map<String, Object> searMap,int page, int size) {
        page = judgePageParameter(page);
        size = judgeSizeParameter(size);
        PageHelper.startPage(page,size);

        Example example=new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        searMap.put("auditStatus",StatusCode.STORE_status_NO);
        criteria.andEqualTo("auditStatus",searMap.get("auditStatus"));

        return (Page<Store>) storeMapper.selectByExample(example);
    }


    /**
     * 审核通过
     * @param id
     * @return
     */
    @Override
    public int updatePass(Long id) {
        Store store = new Store();
        //设置状态
        store.setAuditStatus(StatusCode.STORE_status_YES+"");
        store.setId(id);
        int count = storeMapper.updateByPrimaryKeySelective(store);
        //发送短信
        Example example=new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("contactPhone",store.getContactPhone());
        storeMapper.selectByExample(example);
        System.out.println("很高兴您的信息审核已经通过");
        return count;
    }

    /**
     * 审核未通过
     * @param id
     * @return
     */
    @Override
    public int updateNotPass(Long id) {
        Store store = new Store();
        //设置状态
        store.setAuditStatus(StatusCode.STORE_status_ERRO+"");
        store.setId(id);
        int count = storeMapper.updateByPrimaryKeySelective(store);
        System.out.println("很遗憾您的信息审核失败，请不要气馁，记得检查您的所有的信息是有误或未完善，期待您的下次审核");
        return count;
    }


    /**
     * 判断传入的当前页
     * @param page
     * @return
     */
    public int judgePageParameter(int page) {
        if (page == 0) {
            page = 1;
        }

        String pageString = String.valueOf(page);

        if (pageString == null) {
            pageString = 1 + "";
        }

        page = Integer.parseInt(pageString);

        return page;
    }

    /**
     * 判断传入的显示页
     * @param size
     * @return
     */
    public int judgeSizeParameter(int size) {
        if (size == 0) {
            size = 5;
        }
        String sizeString = String.valueOf(size);
        if (sizeString == null) {
            sizeString = 5 + "";
        }
        size = Integer.parseInt(sizeString);
        return size;
    }


    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */

    private Example exampleBySelect(Map<String, Object> searchMap){
        Example example=new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        //店铺简称
        if(searchMap.get("simpleName")!=null){
            criteria.andEqualTo("name",searchMap.get("name"));
        }

        //主体编码
        if(searchMap.get("code")!=null){
            criteria.andEqualTo("code",searchMap.get("code"));
        }
        //所属类型
        if(searchMap.get("type")!=null){
            criteria.andEqualTo("type",searchMap.get("type"));
        }
        //市
        if(searchMap.get("city")!=null){
            criteria.andEqualTo("city",searchMap.get("city"));
        }
        //区
        if(searchMap.get("county")!=null){
            criteria.andEqualTo("county",searchMap.get("county"));
        }

        //未审核状态
        searchMap.put("auditStatus",StatusCode.STORE_status_NO+"");
        criteria.andEqualTo("auditStatus",searchMap.get("auditStatus"));
        return example;
    }
}
